package com.game.majiang.algorithm.wuziqi;

public class WuziqiLogic {


	public static boolean playChessWin(int[][] qipan,int x,int y)
	{
		int[][] tempqipan =  qipan;
		int qizinumber1 = verifyingUpper_Lower(tempqipan,x,y);
		int qizinumber2 = verifyingUpperleft_Lowerright(tempqipan,x,y);
		int qizinumber3 = verifyingLeft_Right(tempqipan,x,y);
		int qizinumber4 = verifyingUpperright_Lowerleft(tempqipan,x,y);
		if(qizinumber1 >= 4 || qizinumber2 >= 4 || qizinumber3 >= 4 || qizinumber4 >= 4 )
		{
			return true;
		}
		return false;
	}


	//上 -》 下
	public static int verifyingUpper_Lower(int[][] qipan,int x,int y)
	{
		int qizinumber = 0;
		//上
		for (int i = y+1; i < 15; i++) 
		{
			if(i > y)
			{
				if(qipan[x][i] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}
		//下
		for (int i = y-1; i >= 0 ; i--) 
		{
			if(i < y)
			{
				if(qipan[x][i] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}

		return qizinumber;

	}

	//左上  -》 右下
	public static int verifyingUpperleft_Lowerright(int[][] qipan,int x,int y)
	{
		int qizinumber = 0;
		//左上
		for (int i = x-1,j = y+1; i >= 0 && j < 15; i--,j++) 
		{
			if(i < x && j > y)
			{
				if(qipan[i][j] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}
		//右下
		for (int i = x+1,j = y-1; i <15  && j >= 0; i++,j--) 
		{
			if(i > x && j < y)
			{
				if(qipan[i][j] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}
		return qizinumber;
	}
	// 左 -》 右
	public static int verifyingLeft_Right(int[][] qipan,int x,int y) 
	{
		int qizinumber = 0;

		//左
		for (int i = x-1; i > 0; i--) 
		{
			if(i < x)
			{
				if(qipan[i][y] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}
		//右
		for (int i = x+1; i < 15 ; i++) 
		{
			if(i > x)
			{
				if(qipan[i][y] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}

		return qizinumber;
	}
	//左下  -》 右上
	public static int verifyingUpperright_Lowerleft(int[][] qipan,int x,int y)
	{
		int qizinumber = 0;
		//左下
		for (int i = x-1,j = y-1; i >= 0 && j >= 0 ; i--,j--) 
		{
			if(i < x && j < y)
			{
				if(qipan[i][j] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}
		//右上
		for (int i = x+1,j = y+1; i < 15  && j < 15; i++,j++) 
		{
			if(i > x && j > y)
			{
				if(qipan[i][j] == qipan[x][y])
				{
					qizinumber++;
				}
				else
				{
					break;
				}
			}
		}
		return qizinumber;
	}
}
